#include <cstdio>
#include <cassert>

#include <stddef.h>

#include "com_facesdk_FDEyeProcessor.h"
#include "is_facedetect.h"
#include <android/log.h>
#define  LOG_TAG    "gd_eye:"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

static jmethodID econ_id;
static jmethodID sxy_id;
static bool init_eye_status=false;
//EyeLocation eye_location;

static int init_jclass_jmethod(JNIEnv *env)
{
    jclass eye_info_class=env->FindClass("com/facesdk/EyeInfo");
    if(eye_info_class==NULL){
	LOGE("couldn't get EyeInfo class.");
        return -1;
    }
    econ_id=env->GetMethodID(eye_info_class,"<init>","()V");
    if(econ_id == NULL) {
	LOGE("<init> method not fetched.");
    }

    sxy_id=env->GetMethodID(eye_info_class,"setLeftAndRight","(IIII)V");
    if(sxy_id == NULL) {
	LOGE("setLeftAndRight method not fetched.");
    }

    return 0;
}

JNIEXPORT void JNICALL Java_com_facesdk_FDEyeProcessor_EyeInit
(JNIEnv *env, jclass)
{
    FDDLLInfo fddllinfo[10];
    LOGD("initializing FD Eye......\n");
    fflush(stdout);
    if(FDInit(2<<8|1,fddllinfo)!=FD_OK||init_jclass_jmethod(env)!=0){
        init_eye_status=false;
	LOGE("init FD Eye failed.\n");
        return;
    }
    init_eye_status=true;
    LOGD("init FD Eye completed.\n");
    fflush(stdout);
}

JNIEXPORT void JNICALL Java_com_facesdk_FDEyeProcessor_EyeUninit
(JNIEnv *, jclass)
{
   // destroy_eye_location_arr();
}

JNIEXPORT jobject JNICALL Java_com_facesdk_FDEyeProcessor_detectEye
(JNIEnv *env, jclass jobj, jbyteArray image_data, jint width, jint height,
 jint fleft,jint ftop,jint fwidth,jint fheight)
{
    if(!init_eye_status||image_data==NULL) {
	LOGE("status and data are null, return.");
        return NULL;
    }

    jbyte *data=env->GetByteArrayElements(image_data,NULL);
    //unsigned char *data=(unsigned char *)malloc(width*height*sizeof(unsigned char));

    FacePos fl;

    fl.left=fleft;
    fl.top=ftop;
    fl.width=fwidth;
    fl.height=fheight;

    //CPoint left,right;
    EyeInfo eyeInfo;

    int ret=FDEye((unsigned char *)data,width,height,fl,&eyeInfo);

    env->ReleaseByteArrayElements(image_data,data,JNI_ABORT);

    LOGD("ret=%d\n",ret);
    LOGD("eye_info:c++:%d %d %d %d\n",eyeInfo.leftx,eyeInfo.lefty,eyeInfo.rightx,eyeInfo.righty);
#ifndef NDEBUG
    printf("ret=%d\n",ret);
    printf("eye_info:c++:%d %d %d %d\n",eyeInfo.leftx,eyeInfo.lefty,eyeInfo.rightx,eyeInfo.righty);
#endif

    if(ret!=1)
	return NULL;

    jclass eye_info_class=env->FindClass("com/facesdk/EyeInfo");
    if(eye_info_class==NULL){
        LOGE("couldn't get EyeInfo class.\n");
        return NULL;
    }

    jobject eye_info_obj=env->NewObject(eye_info_class,econ_id);
    if(eye_info_obj==NULL){
        LOGE("couldn't new EyeInfo object.\n");
        return NULL;
    }

    env->CallVoidMethod(eye_info_obj,sxy_id,eyeInfo.leftx,eyeInfo.lefty,eyeInfo.rightx,eyeInfo.righty);

    return eye_info_obj;
}

